<?php


namespace App\Http\Controllers\Admin;


use App\Jobs\ProcessPodcast;
use App\Tool\Json;

class EmailController extends AuthCheckController
{


    function readExcel(){

//        $path=

//        dd(request()->getHost());

        $filePath=public_path(). '/uploads/'.request()->input('file');

        $info=pathinfo($filePath);

        $extension=$info['extension'];



        if($extension=='xls'){


            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();


        }elseif($extension=='xlsx'){


            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

        }else{

            return Json::code('请上传正确的文件格式！');

        }


//        if(!$reader) return code(2,'只支持xls和xlsx两种格式的文件！');



        //设置只读
        $reader->setReadDataOnly(true);
        //加载文件
        $spreadsheet = $reader->load($filePath);
        //获取当前工作表并获取最大行数
//        $row = $spreadsheet->getActiveSheet()->getHighestRow();
        $row = $spreadsheet->getActiveSheet()->getHighestDataRow();


        $array=[];

        for ($i = 0; $i < $row - 1; $i++) {


            //从第二行开始
            $email=$spreadsheet->getActiveSheet()->getCell('A' . ($i + 1))->getValue();


            if(!$email) continue;

//            echo $email;

            $array[]=$email;


        }




        return Json::code(1,'success',$array);


    }


    function sendEmail(){

        $post=request()->input();


        $send_time=$post['send_time'];

        $send_time_value=$post['send_time_value'];


        if($send_time==2) $send_time_value=strtotime($send_time_value) - time();


        $send_type=$post['send_type'];

        $email_test=str_replace('，',',',$post['email_test']);

        $email_test=explode(',',$email_test);


        if($send_type==1){


            foreach ($email_test as $v){

                $this->sendWithTest($post['title'],$post['content'],$v,(int)$send_time_value,$post['order']);

            }

            return Json::code(1,'success');

        }


        $current_email=$post['current_email'];

        foreach ($current_email as $v){

            $this->sendWithTest($post['title'],$post['content'],$v,(int)$send_time_value,$post['order']);

        }


//        current_email

        return Json::code(1,'success');







    }



    function sendWithTest($title,$content,$email,$delay=0,$order='default'){

        dispatch(new ProcessPodcast($email,$title,$content))->delay(now()->addSeconds($delay))->onQueue($order);

    }

}
